# coding=utf-8
import pymongo

from center.config import DATABASE_IP, DATABASE_PORT


class MongoClient(object):
    """
    数据库相关操作
    """

    def get_connection(self):
        self.conn = pymongo.Connection(DATABASE_IP, DATABASE_PORT)
        return self.conn

    def get_db(self, dbname):
        self.db = self.conn.dbname
        return self.db

    def disconnect(self):
        self.conn.disconnect()

    def insert(self, table, data):
        self.db[table].insert(data)

    def delete(self, table, data):
        self.db[table].remove(data)

    def find(self, table, query):
        self.db[table].find(query)

    def update(self, table, data):
        self.db[table].update(data)
